package com.module.user.ui.setting

import android.app.Dialog
import android.content.Context
import android.graphics.Color
import android.graphics.Typeface
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.text.TextUtils
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.bigkoo.pickerview.adapter.ArrayWheelAdapter
import com.common.app.data.bean.BaseCityModel
import com.common.app.data.bean.KeyPre
import com.common.base.utils.SpUtilsManagement
import com.common.base.app.extras.setOnClickListener
import com.common.base.utils.ViewUtils.getColor
import com.common.base.utils.ViewUtils.getString
import com.contrarywind.listener.OnItemSelectedListener
import com.module.user.R
import com.module.user.databinding.UserDialogMeteringUnitSelectBinding


/**
 *
 * 作者：sosou
 *
 * 版本：1.0
 *
 * 创建日期：2025/7/7
 *
 * 描述：
 *
 * 修订历史：
 *
 */
class CommonMeteringUnitSelectDialog(context: Context,dataType:Int = 0) : Dialog(context, R.style.custom_dialog_sv),
    View.OnClickListener {
    private var binding: UserDialogMeteringUnitSelectBinding? = null

    /**
     * 点击监听
     */
    private var listener: OnSelectListener? = null


    // 适配器数据
    private var unitAdapter: ArrayWheelAdapter<*>? = null
    private var unitWheelListener: OnItemSelectedListener? = null
    private var unitItems = mutableListOf<BaseCityModel>()
    private var unitItem: BaseCityModel? = null

    private var dataType:Int = 0

    /**
     * +
     * 构造方法
     *
     * @param context
     */
    init {
        this.dataType = dataType
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = UserDialogMeteringUnitSelectBinding.inflate(LayoutInflater.from(context))
        binding?.root?.let { setContentView(it) }

        binding?.let {bindingIt->
            bindingIt.dialogRoot.setOnClickListener(onClick = this)
            bindingIt.negativeButton.setOnClickListener(onClick = this)
            bindingIt.mTitle.text = when(dataType){
                1->getString(R.string.user_setting_unit_weight_hint)
                2->getString(R.string.user_setting_unit_temperature_hint)
                101->getString(R.string.user_goal_setting_ac_modify_sleep_goals_hint)
                102->getString(R.string.user_goal_setting_ac_modify_steps_goals_hint)
                103->getString(R.string.user_goal_setting_ac_modify_calorie_goals_hint)
                104->getString(R.string.user_goal_setting_ac_modify_ac_duration_goals_hint)
                else->getString(R.string.user_setting_unit_length_hint)
            }
        }

    }

    private fun initData() {
        unitItems.clear()
        when(dataType){
            1->{
                unitItems.add(BaseCityModel(name = getString(R.string.common_metering_unit_weight),province = null,pinyin = null,pCode = null,code = "0"))
                unitItems.add(BaseCityModel(name = getString(R.string.common_metering_unit_weight_e),province = null,pinyin = null,pCode = null,code = "1"))
            }
            2->{
                unitItems.add(BaseCityModel(name = getString(R.string.common_metering_unit_tem),province = null,pinyin = null,pCode = null,code = "0"))
                unitItems.add(BaseCityModel(name = getString(R.string.common_metering_unit_tem_e),province = null,pinyin = null,pCode = null,code = "1"))
            }
            101->{//睡眠目标
                unitItems.add(BaseCityModel(name = "4${getString(R.string.common_hours_hint)}0${getString(R.string.common_minutes_hint)}",province = null,pinyin = null,pCode = null,code = "240"))
                unitItems.add(BaseCityModel(name = "4${getString(R.string.common_hours_hint)}30${getString(R.string.common_minutes_hint)}",province = null,pinyin = null,pCode = null,code = "270"))
                unitItems.add(BaseCityModel(name = "5${getString(R.string.common_hours_hint)}0${getString(R.string.common_minutes_hint)}",province = null,pinyin = null,pCode = null,code = "300"))
                unitItems.add(BaseCityModel(name = "5${getString(R.string.common_hours_hint)}30${getString(R.string.common_minutes_hint)}",province = null,pinyin = null,pCode = null,code = "330"))
                unitItems.add(BaseCityModel(name = "6${getString(R.string.common_hours_hint)}0${getString(R.string.common_minutes_hint)}",province = null,pinyin = null,pCode = null,code = "360"))
                unitItems.add(BaseCityModel(name = "6${getString(R.string.common_hours_hint)}30${getString(R.string.common_minutes_hint)}",province = null,pinyin = null,pCode = null,code = "390"))
                unitItems.add(BaseCityModel(name = "7${getString(R.string.common_hours_hint)}0${getString(R.string.common_minutes_hint)}",province = null,pinyin = null,pCode = null,code = "420"))
                unitItems.add(BaseCityModel(name = "7${getString(R.string.common_hours_hint)}30${getString(R.string.common_minutes_hint)}",province = null,pinyin = null,pCode = null,code = "450"))
                unitItems.add(BaseCityModel(name = "8${getString(R.string.common_hours_hint)}0${getString(R.string.common_minutes_hint)}",province = null,pinyin = null,pCode = null,code = "480"))
                unitItems.add(BaseCityModel(name = "8${getString(R.string.common_hours_hint)}30${getString(R.string.common_minutes_hint)}",province = null,pinyin = null,pCode = null,code = "510"))
            }
            102->{//步数目标
                unitItems.add(BaseCityModel(name = "4000",province = null,pinyin = null,pCode = null,code = "4000"))
                unitItems.add(BaseCityModel(name = "5000",province = null,pinyin = null,pCode = null,code = "5000"))
                unitItems.add(BaseCityModel(name = "6000",province = null,pinyin = null,pCode = null,code = "6000"))
                unitItems.add(BaseCityModel(name = "7000",province = null,pinyin = null,pCode = null,code = "7000"))
                unitItems.add(BaseCityModel(name = "8000",province = null,pinyin = null,pCode = null,code = "8000"))
            }
            103->{//卡路里目标
                unitItems.add(BaseCityModel(name = "200",province = null,pinyin = null,pCode = null,code = "200"))
                unitItems.add(BaseCityModel(name = "250",province = null,pinyin = null,pCode = null,code = "250"))
                unitItems.add(BaseCityModel(name = "300",province = null,pinyin = null,pCode = null,code = "300"))
                unitItems.add(BaseCityModel(name = "350",province = null,pinyin = null,pCode = null,code = "350"))
                unitItems.add(BaseCityModel(name = "400",province = null,pinyin = null,pCode = null,code = "400"))
            }
            104->{//活动时长目标
                unitItems.add(BaseCityModel(name = "35",province = null,pinyin = null,pCode = null,code = "35"))
                unitItems.add(BaseCityModel(name = "40",province = null,pinyin = null,pCode = null,code = "40"))
                unitItems.add(BaseCityModel(name = "45",province = null,pinyin = null,pCode = null,code = "45"))
                unitItems.add(BaseCityModel(name = "50",province = null,pinyin = null,pCode = null,code = "50"))
                unitItems.add(BaseCityModel(name = "55",province = null,pinyin = null,pCode = null,code = "55"))
            }
            else->{
                unitItems.add(BaseCityModel(name = getString(R.string.common_metering_unit_length),province = null,pinyin = null,pCode = null,code = "0"))
                unitItems.add(BaseCityModel(name = getString(R.string.common_metering_unit_length_e),province = null,pinyin = null,pCode = null,code = "1"))
            }
        }
    }

    //开始条件选择器初始化
    private fun initStartOptionPicker() {

        binding?.let {

            //字体样式，默认是等宽字体
            it.wvUnit.setTypeface(Typeface.SANS_SERIF)

            //设置滚轮文字大小
            it.wvUnit.setTextSize(17F)

            //设置分割线的颜色
            it.wvUnit.setDividerColor(Color.TRANSPARENT)

            // 设置分割线之间的文字的颜色
            it.wvUnit.setTextColorCenter(getColor(R.color.text_content))

            // 设置分割线以外文字的颜色
            it.wvUnit.setTextColorOut(Color.LTGRAY)

            // 条目间距倍数
            it.wvUnit.setLineSpacingMultiplier(2.6F)

            // 绑定适配器
            unitAdapter = ArrayWheelAdapter(unitItems)
            it.wvUnit.adapter = unitAdapter // 设置显示数据

            val position = when(dataType){
                1-> SpUtilsManagement.getInt(KeyPre.KEY_METERING_UNIT_WEIGHT)
                2-> SpUtilsManagement.getInt(KeyPre.KEY_METERING_UNIT_TEM)
                101-> {
                    val sleepGoalValue = SpUtilsManagement.getInt(KeyPre.KEY_SLEEP_GOALS_VALUE, 360)
                    var foundIndex = 0
                    for (i in unitItems.indices) {
                        if (unitItems[i].code == sleepGoalValue.toString()) {
                            foundIndex = i
                            break
                        }
                    }
                    foundIndex
                }
                102-> {
                    val sleepGoalValue = SpUtilsManagement.getInt(KeyPre.KEY_STEP_TARGET_VALUE, 6000)
                    var foundIndex = 0
                    for (i in unitItems.indices) {
                        if (unitItems[i].code == sleepGoalValue.toString()) {
                            foundIndex = i
                            break
                        }
                    }
                    foundIndex
                }
                103-> {
                    val sleepGoalValue = SpUtilsManagement.getInt(KeyPre.KEY_CALORIE_TARGET_VALUE, 300)
                    var foundIndex = 0
                    for (i in unitItems.indices) {
                        if (unitItems[i].code == sleepGoalValue.toString()) {
                            foundIndex = i
                            break
                        }
                    }
                    foundIndex
                }
                104-> {
                    val sleepGoalValue = SpUtilsManagement.getInt(KeyPre.KEY_AC_DURATION_TARGET_VALUE, 45)
                    var foundIndex = 0
                    for (i in unitItems.indices) {
                        if (unitItems[i].code == sleepGoalValue.toString()) {
                            foundIndex = i
                            break
                        }
                    }
                    foundIndex
                }
                else-> SpUtilsManagement.getInt(KeyPre.KEY_METERING_UNIT_LENGTH)
            }

            it.wvUnit.currentItem = position // 初始化时显示的数据
            unitItem = unitItems[position]


            it.wvUnit.setIsOptions(true)

            it.wvUnit.setCyclic(false)

            it.wvUnit.setItemsVisibleCount(4)

            // 联动监听器
            unitWheelListener = OnItemSelectedListener { index ->
                if (unitItems.size > 0 && !TextUtils.isEmpty(unitItems[index].code) && unitItems.size > index) {
                    unitItem = unitItems[index]
                }
            }
            // 添加联动监听
            it.wvUnit.setOnItemSelectedListener(unitWheelListener)
        }
    }

    override fun onClick(view: View) {
        when (view) {
            binding?.negativeButton -> {
                unitItem?.let {
                    listener?.onSelectClick(dataType,it)
                    SpUtilsManagement.putInt(when(dataType){
                        1->KeyPre.KEY_METERING_UNIT_WEIGHT
                        2->KeyPre.KEY_METERING_UNIT_TEM
                        101->KeyPre.KEY_SLEEP_GOALS_VALUE
                        102->KeyPre.KEY_STEP_TARGET_VALUE
                        103->KeyPre.KEY_CALORIE_TARGET_VALUE
                        104->KeyPre.KEY_AC_DURATION_TARGET_VALUE
                        else->KeyPre.KEY_METERING_UNIT_LENGTH }, it.code.toInt())

                    when(dataType){
                        0->SpUtilsManagement.putString(KeyPre.KEY_METERING_UNIT_LENGTH_NAME, it.name)
                        1->SpUtilsManagement.putString(KeyPre.KEY_METERING_UNIT_WEIGHT_NAME, it.name)
                        2->SpUtilsManagement.putString(KeyPre.KEY_METERING_UNIT_TEM_NAME, it.name)
                        else->{
                        }
                    }
                }
            }
            binding?.dialogRoot -> {
            }
        }
        dismiss()
    }

    override fun show() {
        super.show()
        window?.apply {
            //设置宽度
            attributes.width = ViewGroup.LayoutParams.MATCH_PARENT
            attributes.gravity = Gravity.BOTTOM //设置dialog 在布局中的位置
            setWindowAnimations(com.common.app.R.style.bottom_style_dialog) //添加动画:
            setBackgroundDrawable(ColorDrawable(getColor(R.color.transparent))) // android:windowBackground
        }
        initData()
        initStartOptionPicker()
    }

    fun show(mType:Int = 0) {
        dataType = mType
        show()
    }

    override fun dismiss() {
        super.dismiss()
    }

    /**
     * @param listener
     */
    fun setListener(listener: OnSelectListener?) {
        this.listener = listener
    }

    /**
     *
     */
    interface OnSelectListener {
        fun onSelectClick(mType:Int = 0, mBean: BaseCityModel)
    }
}